package com.software.process.old

import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions.avg

import java.io.{BufferedWriter, File, FileWriter}

object CalculateAvg {
  def main(args: Array[String]) {
    val spark = SparkSession.builder().appName("CalculateAvg").master("local[*]").config("spark.testing.memory", "2147480000").getOrCreate()
    val data = spark.read.option("header", "true").option("delimiter", "\t").csv("input\\all_aqi.csv")
    val averages = data.columns.tail.map(c => data.agg(avg(c)).first.get(0))
    val result = averages.map(_.toString).reduce(_ + "\t" + _)
    val file = new File("input\\all_aqi2.csv")
    val bw = new BufferedWriter(new FileWriter(file))
    bw.write(result)
    bw.close()
    spark.stop()
  }
}
